package com.upstream.link.control
{
	import com.upstream.link.SYS;
	import com.upstream.link.event.GameEvent;
	import com.upstream.link.model.GameModel;
	import com.upstream.link.view.GameView;
	import com.upstream.link.view.RectItem;
	
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.utils.getTimer;
	
	public class GameController
	{
		private var _gameView:GameView;
		private var _crtItemArr:Array;
		
		private var _crtItem1:RectItem;
		private var _crtItem2:RectItem;
		public function GameController()
		{
			this._crtItemArr = new Array();
			this._gameView = new GameView(GameModel.getInstance().rectVoVect); 
			SYS.STAGE.addChild(this._gameView);
			
			for each(var item:RectItem in this._gameView.itemArr){
				item.addEventListener(MouseEvent.CLICK,itemCKHandler);
			}
		}
		
		private function itemCKHandler(e:MouseEvent):void
		{
			var item:RectItem = e.currentTarget as RectItem;
			if(item.rectVo.type == 0) return;
			if(_crtItem1 == item){
				_crtItem1.filters = [];
				_crtItem1 = null;
				return;
			}
			if(_crtItem2 == item){
				_crtItem2.filters = [];
				_crtItem2 = null;
				return;
			}
			if(_crtItem1 == null){
				_crtItem1 = item;
				_crtItem1.filters = [new GlowFilter(0xffffff,1,6,6,2,1,true)];
			}else if(_crtItem2 == null){
				_crtItem2  =item;
				_crtItem2.filters = [new GlowFilter(0xffffff,1,6,6,2,1,true)];
			}else{
				_crtItem1.filters = _crtItem2.filters = []; 
				_crtItem2 = null;
				_crtItem1 = item;
				_crtItem1.filters = [new GlowFilter(0xffffff,1,6,6,2,1,true)];
			}
			
			checkPath();
		}
		private function checkPath():void{
			if(_crtItem1 == null || _crtItem2 == null) return;
			if(_crtItem1.rectVo.type != _crtItem2.rectVo.type) return;
			if(new PathLogic().checkPoint(_crtItem1.rectVo.p,_crtItem2.rectVo.p)){
				trace("OK!");
				_crtItem1.filters = _crtItem2.filters = []; 
				_crtItem1.changeType();
				_crtItem1 = null;
				_crtItem2.changeType();
				_crtItem2 = null;
			}else{
				trace("NO!");
			}
			
		}
	}
}
